package com.sanen.flyer.factory;

import com.sanen.flyer.entity.blast.Blast;
import com.sanen.flyer.entity.blast.EnemyBlast;
import com.sanen.flyer.entity.blast.bulletBlast;
import com.sanen.flyer.tool.Console;
import com.sanen.flyer.tool.ImageTool;

import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.List;

/**
 * 爆炸工厂
 * @author wsse
 * @date 2018-09-20 16:14
 */
public class BlastFactory {
    //存储使用后移除的爆炸对象,便于重复利用
    private static List<Blast> enemyList;
    private static List<Blast> bulletList;
    public static final int BLAST_ENEMY = 1;
    public static BufferedImage blastEnemy;
    public static final int BLAST_BULLET = 2;
    public static BufferedImage bulletEnemy;

    public static Blast getBlast(int type, int x, int y) {
        switch (type) {
            case BLAST_ENEMY:
                return getBlastEnemy(x, y);
            case BLAST_BULLET:
                return getBlastBullet(x, y);
        }
        return null;
    }

    //飞机爆炸图
    public static synchronized BufferedImage getBlastEnemyImg() {
        if (blastEnemy == null) blastEnemy = ImageTool.getImage("./image/blast/blast_enemy.png");
        return blastEnemy;
    }

    //    子弹击中效果图
    public static synchronized BufferedImage getBulletEnemyImg() {
        if (bulletEnemy == null) bulletEnemy = ImageTool.getImage("./image/blast/blast_bullet.png");
        return bulletEnemy;
    }

    /**
     * 获取飞机爆炸效果
     */
    public static synchronized Blast getBlastEnemy(int x, int y) {
        if (enemyList == null) {
            enemyList = new ArrayList<>();
        }
        if (enemyList.size() == 0) {
            return new EnemyBlast(getBlastEnemyImg(), x, y);
        }
        Blast blast=enemyList.remove(0);
        blast.init(x,y);
        return blast;
    }

    /**
     * 获取子弹击中爆炸效果
     */
    public static synchronized Blast getBlastBullet(int x, int y) {
        if (bulletList == null) {
            bulletList = new ArrayList<>();
        }
        if (bulletList.size() == 0) {
            return new bulletBlast(getBulletEnemyImg(), x, y);
        }
        Blast blast=bulletList.remove(0);
        blast.init(x,y);
        return blast;
    }

    /**
     * 回收游戏移除的爆炸
     * @param blast
     */
    public static void recycle(Blast blast) {
        if (blast instanceof EnemyBlast) {
            enemyList.add(blast);
        } else if (blast instanceof bulletBlast) {
            bulletList.add(blast);
        }
    }

}
